<?php
	function mostrarComentariosPartido($idPartido){
		$q = "SELECT * FROM (
						(SELECT 	c.id as id, 
								c.fecha as fecha, 
								c.usuario as usuario, 
								c.texto as texto,
								c.video as video, 
								c.partido as partido, 
								u.jugador as jugador, 
								u.usuario as username,	
								NULL as image, 
								NULL as thumb, 
								NULL as image_width, 
								NULL as image_height, 
								NULL as thumb_width, 
								NULL as thumb_height								
								FROM comentarios_partido as c, usuarios as u   
								WHERE c.partido = '".$idPartido."' AND 
								u.id = c.usuario AND c.imagen = '0')
				UNION (
						SELECT c.id as id, 
								c.fecha as fecha, 
								c.usuario as usuario, 
								c.texto as texto, 
								c.video as video, 
								c.partido as partido, 
								u.jugador as jugador, 
								u.usuario as username,	
								i.picture_path as image, 
								i.thumbnail_path as thumb, 
								i.width as image_width, 
								i.height as image_height, 
								i.thumbnail_width as thumb_width, 
								i.thumbnail_height as thumb_height								
								FROM comentarios_partido as c, usuarios as u, image as i    
								WHERE c.partido = '".$idPartido."' AND 
								u.id = c.usuario AND c.imagen = i.id)) as Q ORDER BY fecha ASC";
		$query = new mysqlQuery("tournament", $q, "", "", "", "", "");
		$query->execute();
		$number = 0;
		if($query->getCount() > 0){
			while($row = $query->next()){
				$number ++;
				mostrarComentario($row, $number, isAdmin());
			}
		}else{
			echo '<p class="no-comments" style="font-size:10pt; font-weight: bold">No comments for this match</p>';
		}
	}
	
	function mostrarComentario($data, $number, $admin){
		?>
		<div class="comment">
			<?php 
				if($admin){
					echo '<a href="#" class="delete">Delete This</a>';
				}
			?>			
			<a href="#<?php echo $number;?>" name="<?php echo $number;?>" class="number">#<?php echo $number;?></a>
			<div class="author"><?php echo $data["jugador"]?></div>
			<div class="date"><?php echo date("d.m.Y h:m", $data["fecha"]);?></div>
			<div class="text"><?php echo nl2br(utf8_decode($data["texto"]));?></div>
			<?php 
				if($data["image"]){
					echo '<a href="'.$data["image"].'" class="image" target="_blank">Image Attached</a>';
				}
				if(strlen($data["video"]) > 0){
					echo '<a href="'.$data["video"].'" class="video" target="_blank">Video Attached</a>';
				}
			?>
		</div>
		<?php
	}
	
	function isAllowedToPostMatchComments($data){
		if(	($data["e1j1"] == $_SESSION["user_id"] && $data["e1j1usuario"] == $_SESSION["username"]) || 
			($data["e1j2"] == $_SESSION["user_id"] && $data["e1j2usuario"] == $_SESSION["username"]) ||
			($data["e2j1"] == $_SESSION["user_id"] && $data["e2j1usuario"] == $_SESSION["username"]) ||
			($data["e2j2"] == $_SESSION["user_id"] && $data["e2j2usuario"] == $_SESSION["username"]) || 
			$_SESSION["level"] == "1"){
			return TRUE;
		}else{
			return FALSE;
		}
	}
?>